package com.hk.hkvedio.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hk.hkvedio.config.MyWebSocket;
import com.hk.hkvedio.model.Record;
import com.hk.hkvedio.monitor.HkVideoMonitor;
import com.hk.hkvedio.service.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "record")
@Slf4j
public class RecordController {

    @Autowired
    private RecordService recordService;

    @PostMapping(value = "add")
    public Object add(@RequestBody Record record) {
        record.setCreateTime(new Date());
        return recordService.save(record);
    }


    @GetMapping(value = "page")
    public Object page(Integer page, Integer limit, String startTime, String endTime) {

        int i = (page - 1) * limit;
        List<Record> list = recordService.list(new LambdaQueryWrapper<Record>()
                .ge(startTime != null, Record::getCallTime, startTime + " 00:00:00")
                .lt(endTime != null, Record::getCallTime, endTime + "  23:59:00")
                        .orderByDesc(Record::getCreateTime)
                .last("limit " + i + "," + limit));
        long count = recordService.count(new LambdaQueryWrapper<Record>()
                .ge(startTime != null, Record::getCallTime, startTime + " 00:00:00")
                .lt(endTime != null, Record::getCallTime, endTime + "  23:59:00"));

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "查询成功");
        result.put("count", count);
        result.put("data", list);
        return result;
    }

    @Autowired
    MyWebSocket myWebSocket;


    @GetMapping(value = "test")
    public void test() {
        Map<String, String> mm = new HashMap<>(18);
        mm.put("id", "100");
        mm.put("time", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        mm.put("type", "入侵报警");
        mm.put("ip", "192.168.0.11");
       myWebSocket.baojing(JSONObject.toJSONString(mm));
    }



}
